/**
 * Features Required:
 * 
 * 1. 
 * 
 */

if (!dojo._hasResource["dojoide.workspace.AutoCompletePane"]) {
    dojo._hasResource["dojoide.workspace.AutoCompletePane"] = true;
    dojo.provide("dojoide.workspace.AutoCompletePane");
    
	dojo.require('dojo.dnd.move');

	dojo.require('dojoide.workspace.OutputPane');

    //OutlinePane
    dojo.declare("dojoide.workspace.AutoCompletePane", dojoide.workspace.OutputPane, {
        listedObjects: [],
        selectedItem: null,
        
        constructor: function(){
            if (!dojoide.workspace.AutoCompletePane.REQUESTS_Q) {
                dojoide.workspace.AutoCompletePane.REQUESTS_Q = [];
            }
            this.duration = 250;
            this.open = true;
	        this._interval = 10;
	        this._scrollPos = 1;
            setInterval(this._processQueue, this._interval);
        },
        
        postCreate: function(){
            this.inherited('postCreate', arguments);
        },
        
        clear: function(){
        	this.inherited('clear', arguments);
        	this.listedObjects = [];
        	this.selectedItem = null;
        	this._scrollPos = 1;
        },
        
        show: function(/* number */ posX, /* number */ posY){
            if (posX != null && posY != null) {
            	this.domNode.style.position = "absolute";
            	this.domNode.style.top = posX + '';
            	this.domNode.style.left = posY + '';
            };
        	this.inherited('show', arguments);
        },
        
        itemClicked: function(itemIndex){
        	this.selectItemByIndex(itemIndex);
        },
        
        selectItemByIndex: function(itemIndex){
			this.selectItem(this.listedObjects[itemIndex]);
        },
        
        selectItem: function(itemToSelect){
            if(itemToSelect == null){
            	return;
            }
            var divItemToSelect = dojo.byId((itemToSelect.itemIndex+''), this.document);
            if(divItemToSelect == null){
            	return;
            }
            
        	//deselect old item and select the new one
        	if (this.selectedItem != null){
        		var divSelectedItem = dojo.byId((this.selectedItem.itemIndex+''), this.document);
            	divSelectedItem.style.backgroundColor = '';
        	}
        	divItemToSelect.style.backgroundColor = 'lightgrey';
        	this.selectedItem = itemToSelect;
        	
        	//scroll if needed
        	var visibleAreaStart = this.document.body.scrollTop;
        	var visibleAreaEnd = this.document.body.scrollTop + this.iframe.offsetHeight;
        	var itemStart = divItemToSelect.offsetTop;
        	var itemEnd = divItemToSelect.offsetTop + divItemToSelect.offsetHeight;
        	if (itemEnd > visibleAreaEnd){
        		this._scrollPos = visibleAreaStart + (itemEnd - visibleAreaEnd);
            	this.window.scroll(0, this._scrollPos);
        	} else if (itemStart < visibleAreaStart){
        		this._scrollPos = itemStart;
            	this.window.scroll(0, this._scrollPos);
        	}
        },
        
        moveSelection: function(moveSize, itemIndex){
	    	if(moveSize != null){
	    		//moveSize may be -ve or +ve to move backward or forward respectively
	    		var selectedItemIndex = this.selectedItem == null?-1:this.selectedItem.itemIndex;
	        	selectedItemIndex = (selectedItemIndex + moveSize) % this.listedObjects.length;
	    		if (selectedItemIndex < 0){
	    			selectedItemIndex += this.listedObjects.length;
	    		}
	    	} else if (itemIndex != null){// move to specific index
	        	selectedItemIndex = itemIndex;
	    	} else {//default behavior - move one step forward
	        	selectedItemIndex = (selectedItemIndex + 1) % this.listedObjects.length;
	    	}
	    	this.selectItem(this.listedObjects[selectedItemIndex]);
        },
        
        _addToRequestsQueue: function(request){
            dojoide.workspace.AutoCompletePane.REQUESTS_Q.push(request);
        },
        
        _processQueue: function(){
        	//list objArr array items
            if (dojoide.workspace.AutoCompletePane.REQUESTS_Q.length <= 0){
                return;
            }
            var myRequest = dojoide.workspace.AutoCompletePane.REQUESTS_Q.shift();
            if (myRequest.clearList && myRequest.clearList == true) {
                myRequest.pane.clear();
            }
            if (myRequest.listTitle && myRequest.listTitle != '') {
            	if(!myRequest.pane.isEmpty()){
            		myRequest.pane.append('<div>&nbsp;</div>');
            	}
                myRequest.pane.append('<div>&nbsp;<b>' + myRequest.listTitle + '</b></div>');
            }
            if (myRequest.objArr != null && myRequest.objArr.length > 0){
	            for (var i=0; i < myRequest.objArr.length; i++) {
	            	var obj = myRequest.objArr[i];
	            	obj.itemIndex = myRequest.pane.listedObjects.length + i;
	            	if (myRequest.asErrors != null && myRequest.asErrors == true){
		                myRequest.pane.append('<div style="color: red;" onclick="parent.dijit.byId(\''+myRequest.pane.id+'\').itemClicked('+obj.itemIndex+')" onmouseover="this.style.cursor=\'default\'" id="'+ obj.itemIndex +'">&nbsp;' + obj + '</div>');
	            	} else {
		                myRequest.pane.append('<div onclick="parent.dijit.byId(\''+myRequest.pane.id+'\').itemClicked('+obj.itemIndex+')" onmouseover="this.style.cursor=\'default\'" id="'+ obj.itemIndex +'">&nbsp;' + obj + '</div>');
	            	}
	            }
	            myRequest.pane.listedObjects = myRequest.pane.listedObjects.concat(myRequest.objArr);
            }
            myRequest.pane.updateView();
            if (myRequest.itemToSelect != null){
            	myRequest.pane.selectItem(myRequest.itemToSelect);
            }
            if (myRequest.pane._scrollPos){
            	myRequest.pane.window.scroll(0, myRequest.pane._scrollPos);
            }
        },
        
        list: function(objArr, clearList, listTitle, itemToSelect, asErrors){
            if (!objArr) {
                return;
            }
            this._addToRequestsQueue({
                'pane': this,
                'clearList': clearList,
                'listTitle': listTitle,
                'objArr': objArr,
                'itemToSelect': itemToSelect,
                'asErrors': asErrors
            });
        }
        
    });
}
